<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 14-10-23
 * Time: 下午6:10
 */

class curl38 {
    var $cookie;
    function curl38($url,$cookie,$Referer="",$Querystring="",$json=0){
        $timeout=15;
        if($cookie!="") $this->cookie=$cookie;
        $ch = curl_init();
        curl_setopt ($ch, CURLOPT_URL, $url);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)");
        curl_setopt($ch, CURLOPT_HEADER, 1);
        if($Querystring!="") curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        if($Referer!="") curl_setopt($ch, CURLOPT_REFERER, $Referer);
        if($cookie!="") curl_setopt($ch, CURLOPT_COOKIE, $cookie);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
        if($Querystring!="") curl_setopt($ch, CURLOPT_POSTFIELDS, $Querystring);
        if($json==1){
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8',
                'X-Requested-With: XMLHttpRequest',
                'Accept: application/json, text/javascript, * / *; q=0.01',
                'Accept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3',
                'Connection: Keep-Alive',
            ));
        }elseif($json==2){
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8',
                'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,* / *;q=0.8',
                'Accept: application/json, text/javascript, * / *; q=0.01',
                'Accept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3',
                'Connection: Keep-Alive',
            ));
        }elseif($json==3){
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Accept: text/html, application/xhtml+xml, */*',
                'Accept-Language: zh-cn',
                'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko',
                'Accept-Encoding: gzip, deflate',
                'Connection: Keep-Alive',
                'DNT: 1',
            ));
        }
        if($json==1||$json==2||$json==3){
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
            ob_start();
            echo curl_exec($ch);
            curl_close($ch);
            $result = ob_get_contents();
            ob_end_clean();
        }else{
            $result=curl_exec($ch);
            curl_close($ch);
        }

        preg_match("/(^.*)(?:\r\n\r\n|\n\n)(.*?)$/Uis", $result, $nn);
        $this->header=$nn[1];
        $result=$nn[2];

        if(preg_match("/^Location:\s*([^\r\n]+)/imx",$this->header,$nn)){
            $this->clcookie();
            $nurl=trim(self::quanurl($nn[1],$url));
            $nextp=new curl38($nurl,$this->cookie,$url);
            $result=$nextp->htm;
        }
        if (@gzinflate(substr($result, 10))) {
            $result = @gzinflate(substr($result, 10));
        }
        $this->htm=$result;
        $this->clcookie();
    }

    function clcookie(){
        preg_match_all("/Set-Cookie\:\s+(.*)(?:$|(?:path|domain|expires)\s*=)/Uimx",$this->header,$nn);
        if(count($nn[1])==0) return;
        $cstr=";".$this->cookie.";";
        for($i=0;$i<count($nn[1]);$i++){
            $cstr.=";".trim($nn[1][$i]);
        }
        $str=preg_replace("/\;+/",";",$cstr);
        $str=preg_replace("/^\;/","",$str);
        $nn=explode(";",$str);
        $arr=array();
        $arr2=array();
        foreach($nn as $k=>$v){
            $ar=explode("=",$v);
            if(trim($ar[0])!=""){
                $arr2[trim($ar[0])]=trim($ar[1]);
            }
        }
        $s="";
        foreach($arr2 as $k=>$v){
            $s.=$k."=".$v.";";
        }
        $this->cookie=$s;
    }


    function quanurl($subUrl, $wurl, $baseurl = "")
    {
        if (is_array($subUrl)) return;
        $baseurl=preg_replace("/(http\:\/\/[^\/]+)$/Uisx","\\1/",$baseurl);
        $subUrl = str_replace('\\', "/", $subUrl);
        $subUrl = str_replace('&', "&", $subUrl);
        $subUrl = preg_replace("/^\.\?/", "?", $subUrl);
        $wurl = ($baseurl) ? $baseurl : $wurl;
        $wurl = preg_replace("/\?.*$/Uisx", "", $wurl);
        preg_match_all("/(http\:\/\/([^\/]*?))(.*)$/Uimx", $wurl, $mm);
        $host = $mm[2][0];

        if (substr($subUrl, 0, 7) == "http://") {
            $newurl = $subUrl;
        } elseif (preg_match("/^\//", $subUrl)) {
            $newurl = "http://" . $host . $subUrl;
        } elseif (preg_match("/^\.\.\//", $subUrl)) {
            $tmsuburl = $subUrl;
            $tempparentUrl = $wurl;
            preg_match_all("/(\.\.\/)/Uisx", $subUrl, $nn);
            for ($i = 0; $i < count($nn[1]); $i++) {
                $tmsuburl = preg_replace("/^\.\.\//", "", $tmsuburl);
                $tempparentUrl = preg_replace("/(?!<\/)\/[^<>\/]*\/[^<>\/]*$/", "/", $tempparentUrl);
            }
            $newurl = preg_replace("/\/$/", "", $tempparentUrl) . "/" . $tmsuburl;
        } elseif (preg_match("/^[^\.\/\?]/", $subUrl)) {
            preg_match("/^(.*)\//Uisx",$subUrl,$nna);
            preg_match("/http\:\/\/.*\/(.*)\//Uisx",$wurl,$nnb);
            if($nnb[1]==$nna[1]){
                $newurl = "http://" . $host ."/". $subUrl;
            }else{
                $newurl = dirname2($wurl) . "/" . $subUrl;
            }
        } elseif (preg_match("/^\?/", $subUrl)) {
            $newurl = preg_replace("/\?.*$/U", "", $wurl) . $subUrl;
        } else {
            $newurl = preg_replace("/\/[^\/]*$/", "/", $wurl) . $subUrl;
        }
        $newurl = preg_replace("/\/\.\//", "/", $newurl);
        $newurl = preg_replace("/\/http\:\//", "/", $newurl);
        return $newurl;
    }
}

function r($nr){
    $fn=fopen("d:/1.txt","w");fwrite($fn,$nr);fclose($fn);
    $fn=fopen("d:/TMPTMP_ytempnr.htm.txt","w");fwrite($fn,$nr);fclose($fn);
}

function host($str)
{
    preg_match_all("/http\:\/\/(?:[^<>\.]+\.)??([^<>\/]+(?<!www)(\.(com|gov|edu|org)(?=\/|$|\.cn)|\.cn(?=\/|$|\:)|\.net|\.cc|\.tv|\.hk|\.tw)+?)/Uisx", $str, $nn);
    $host = $nn[1][0];
    $host = preg_replace("/^(?:[^<>]*\.)*([a-z0-9_]+\.(com|gov|edu|org|cn))/Uisx", "\\1", $host);
    return $host;
}

function whost($str){
    preg_match_all("/http\:\/\/([^\/]+)/",$str,$nn);
    $whost=$nn[1][0];
    return $whost;
}